# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, pysozluk-qt Development Team
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

from os import path, mkdir
from ConfigParser import RawConfigParser
from gettext import translation
import pysozlukglobals

_ = translation('pysozluk-qt', fallback=True).ugettext

class pysozlukConfiguration:
    """This class holds configuration values for pysozluk"""
    class general:
        offline = False

    def __init__(self):
        self.config = RawConfigParser()
        self.read()

    def read(self, configFile = pysozlukglobals.config):
        """Reads configuration from config file"""
        self.config.read([configFile])
        self.fixConfig()
        if self.config.get("general", "offline") == "True":
            self.general.offline = True
        else:
            self.general.offline = False

    def fixConfig(self):
        """Fixes configuration if there's something to fix"""
        # check sections and items
        if not self.config.has_section("general"):
            self.config.add_section("general")
        if not self.config.has_option("general", "offline"):
            self.config.set("general","offline","False")
        # check for items with wrong values
        if not self.config.get("general", "offline") in ["True", "False"]:
            self.config.set("general", "offline", self.general.offline.__str__())

    def write(self, configFile = pysozlukglobals.config):
        """Writes current configuration to config file"""
        # read current configuration
        self.config.set("general", "offline", self.general.offline.__str__())
        # write to file
        try:
            if not path.exists(pysozlukglobals.configDirectory):
                mkdir(pysozlukglobals.configDirectory)
            f = file(configFile, "w")
            self.config.write(f)
            f.close()
        except:
            print _("Error") + ": " + _("pysozluk-qt couldn't write a config file in your home directory, please check permissions")
